package com.modern.js.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modern.js.entity.Business;
import com.modern.js.entity.Department;
import com.modern.js.service.IBusinessService;
import com.modern.js.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author PIGS
 * @since 2020-03-28
 */
@RestController
@RequestMapping("/business")
public class BusinessController {
    @Autowired
    private IBusinessService iBusinessService;


    @GetMapping("/list")
    public Map getList(Integer page, Integer limit,String busubessName,String createTime){
        Map<String,Object> map = new HashMap<String,Object>();
        Page<Business> businessPage = new Page<Business>();
        businessPage.setSize(limit);//设置每页记录数
        businessPage.setCurrent(page);//设置当前页码
        IPage<Business> iPage =iBusinessService.selectAll(businessPage,busubessName,createTime);
        if (iPage.getTotal()!=0){
            map.put("msg","查询成功！");
        }else {
            map.put("msg","查询失败！");
        }
        map.put("data",iPage.getRecords());
        map.put("count",iPage.getTotal());
        map.put("code",0);
        return map;
    }

    @DeleteMapping("/del/{businessId}")
    public boolean delBusiness(@PathVariable Integer businessId){
        Business business = new Business();
        business.setBusinessId(businessId);
        business.setIsDel(0);
        boolean result = iBusinessService.updateById(business);
        return result;
    }
    @PostMapping("/add")
    public boolean addBusiness(Business business){
        business.setCreateTime(StringUtils.getNowTime());
        business.setUpdateTime(StringUtils.getNowTime());
        business.setIsDel(1);
        boolean result = iBusinessService.save(business);
        return result;
    }
    @PutMapping("/update")
    public boolean updateBusiness(Business business){
        business.setUpdateTime(StringUtils.getNowTime());
        boolean result = iBusinessService.updateById(business);
        return result;
    }

    @GetMapping("/selectBusiness")
    public List selectBusiness(){
        List<Business> list = iBusinessService.list();
        for (Business business:list){
            System.out.println(business);
        }
        return list;
    }
}
